package com.msh.frame.common.util;


import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;


/**
 * @author shihu
 */
@Slf4j
public class HttpClientUtil {

  private final static int CONNECT_TIMEOUT = 3000;
  private final static int READ_TIMEOUT = 20000;


  private static HttpURLConnection getConnection(String url) throws IOException {
    URL httpUrl = new URL(url);
    HttpURLConnection httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
    httpURLConnection.setConnectTimeout(CONNECT_TIMEOUT);
    httpURLConnection.setReadTimeout(READ_TIMEOUT);
    httpURLConnection.setRequestProperty("accept", "*/*");
    httpURLConnection.setRequestProperty("connection", "Keep-Alive");
    return httpURLConnection;
  }

  public static String get(final String url) {
    StringBuffer stringBuffer = null;
    BufferedReader bufferedReader = null;
    InputStream inputStream = null;
    try {
      HttpURLConnection connection = getConnection(url);
      connection.setDoOutput(false);
      connection.setDoInput(true);
      connection.setRequestMethod("GET");
      connection.connect();
      inputStream = connection.getInputStream();
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
      stringBuffer = new StringBuffer();
      String line = null;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line);
      }
      return stringBuffer.toString();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (stringBuffer != null) {
        try {
          bufferedReader.close();
        } catch (IOException var18) {
          var18.printStackTrace();
        }
      }
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException var17) {
          var17.printStackTrace();
        }
      }
    }
    return null;
  }

  public static String post(final String url, String body) {
    return post(url, body, new HashMap<>(16));
  }

  public static String post(final String url, String body, Map<String, String> header) {
    OutputStream outputStream = null;
    StringBuffer stringBuffer = null;
    BufferedReader bufferedReader = null;
    InputStream inputStream = null;
    try {
      HttpURLConnection connection = getConnection(url);
      connection.setRequestMethod("POST");
      connection.setDoOutput(true);
      if (header != null) {
        header.forEach(new BiConsumer<String, String>() {
          @Override
          public void accept(String name, String value) {
            connection.setRequestProperty(name, value);
          }
        });
      }
      connection.setDoInput(true);
      connection.connect();
      outputStream = connection.getOutputStream();
      outputStream.write(body.getBytes("UTF-8"));
      inputStream = connection.getInputStream();
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
      stringBuffer = new StringBuffer();
      String line = null;
      while ((line = bufferedReader.readLine()) != null) {
        stringBuffer.append(line);
      }
      return stringBuffer.toString();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (stringBuffer != null) {
        try {
          bufferedReader.close();
        } catch (IOException var18) {
          var18.printStackTrace();
        }
      }
      if (inputStream != null) {
        try {
          inputStream.close();
        } catch (IOException var17) {
          var17.printStackTrace();
        }
      }
      if (outputStream != null) {
        try {
          outputStream.close();
        } catch (IOException var16) {
          var16.printStackTrace();
        }
      }
    }
    return null;
  }

/*    public static <T extends BasePO> T get(final String url, Class<T> clazz) {
        String response = get(url);
        if (StringUtil.isNotEmpty(response)) {
            return JSONObject.parseObject(response,clazz);
        }
        return null;
    }

    public static <T extends BasePO> T post(final String url, String body, Class<T> clazz) {
        String response = post(url, body, new HashMap<>());
        if (StringUtil.isNotEmpty(response)) {
            return JSONObject.parseObject(response,clazz);
        }
        return null;
    }*/

  public static byte[] download(String url) {
    try {
      ByteArrayOutputStream outputStream = null;
      InputStream inputStream = null;
      HttpURLConnection connection = getConnection(url);
      if (connection != null) {
        try {
          connection.setRequestMethod("GET");
          connection.setRequestProperty("Content-Type",
              "application/x-www-form-urlencoded");
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.connect();
          inputStream = connection.getInputStream();
          outputStream = new ByteArrayOutputStream();
          byte[] buffer = new byte[16];
          int len = 0;
          while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
          }
          return outputStream.toByteArray();
        } catch (Exception e) {
          log.error("http_client_error " + url + ":{}", e);
        } finally {
          if (inputStream != null) {
            try {
              inputStream.close();
            } catch (IOException var17) {
              var17.printStackTrace();
            }
          }
          if (outputStream != null) {
            try {
              outputStream.close();
            } catch (IOException var16) {
              var16.printStackTrace();
            }
          }
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }


}
